package com.fieldchain.ex.fa100.ui.trade.activity;

import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.CoinSufBean;
import com.fieldchain.ex.fa100.bean.event.MainEvent;
import com.fieldchain.ex.fa100.inter.IntegerCallBack;
import com.fieldchain.ex.fa100.ui.trade.adapter.ChoseMarketAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/26
 */
public class ChoseMarketActivity extends MyBaseActivity {
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private TabLayout table;
    private RecyclerView rv;
    private ChoseMarketAdapter adapter;
    private List<CoinSufBean> mList = new ArrayList<>();
    public String market = "";
    public String company = "";
    public String coin = "";
    private int index;
    private int pageType=0;

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        table = findViewById(R.id.table);
        rv = findViewById(R.id.rv);
    }

    @Override
    public void setValue() {
        pageType=getIntent().getIntExtra("page",0);
        mList= (List<CoinSufBean>) getIntent().getSerializableExtra("list");
        market=getIntent().getStringExtra("market")+"";
        String[] tags = market.split("_");
        coin = tags[1];
        company = tags[0];

        tvCenter.setText("交易市场");

        for(int i=0;i<mList.size();i++){
            table.addTab(table.newTab().setText(mList.get(i).getCoinSuf()));
            if(coin.equals(mList.get(i).getCoinSuf())){
                table.getTabAt(i).select();
                index=i;
            }
        }
        table.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                coin=tab.getText().toString();
                for(int i=0;i<mList.size();i++) {
                    if (coin.equals(mList.get(i).getCoinSuf())) {
                        index = i;
                    }
                }
                adapter.setData(mList.get(index).getListMarket(),market);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        adapter=new ChoseMarketAdapter(mContext, new IntegerCallBack() {
            @Override
            public void index(int pos) {
                String msg=mList.get(index).getListMarket().get(pos);
                EventBus.getDefault().post(new MainEvent(1,pageType,msg));
                finish();
            }
        });
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.setAdapter(adapter);
        adapter.setData(mList.get(index).getListMarket(),market);


        reaBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public int getLayout() {
        return R.layout.activity_chosemarket;
    }
}
